<template>
  <div style="height:100%">
    <el-form ref="form" :model="form" :rules="rules" :disabled="!form.busPageInfo.editable" size="mini" label-width="160px">
      <el-row :gutter="2">
        <el-col :span="6">
          <el-form-item label="组织机构" prop="busMainData.sysOrgCode">
            <OrgsetTreeSelect v-model="form.busMainData.sysOrgCode" :editable="form.busPageInfo.editable && form.busMainData.funProcess === '10'" :has-priv="false" style="width: 200px" />
          </el-form-item>
          <el-form-item label="业务时间" prop="busMainData.busDate">
            <el-date-picker
              v-model="form.busMainData.busDate"
              placeholder="业务时间"
              type="datetime"
              size="mini"
              clearable
              style="width: 200px"
              class="filter-item" />
          </el-form-item>
          <el-form-item label="退换货" prop="busMainData.billType">
            <el-select v-model="form.busMainData.billType" :style="style" size="mini" filterable placeholder="退换货类型">
              <el-option
                v-for="(item) in dicts.sale_stock_billtype"
                :key="item.id"
                :label="item.dictName"
                :value="item.dictCode"/>
            </el-select>
          </el-form-item>
          <el-form-item label="物流公司/单号" prop="busMainData.logisticsCpy">
            <el-select v-model="form.busMainData.logisticsCpy" style="width:100px;" size="mini" filterable placeholder="物流公司">
              <el-option
                v-for="(item) in dicts.logistics_cpy"
                :key="item.id"
                :label="item.dictName"
                :value="item.dictCode"/>
            </el-select>
            <el-input
              v-model="form.busMainData.logisticsNo"
              style="width:180px;"
              size="mini"
              class="filter-item" />
          </el-form-item>
          <el-form-item label="物流状态" prop="busMainData.logisticsStatus">
            <template v-for="(item) in dicts.logistics_status">
              <el-tag v-if="item.dictCode === form.busMainData.logisticsStatus" :type="item.attribute1" :key="item.id">{{ item.dictName }}</el-tag>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户" prop="busMainData.otherSysOwnerCpy">
            <CpyOrgSetSelectDialog
              v-model="form.busMainData.busOtherCpyName"
              :editable="form.busPageInfo.editable && form.busMainData.funProcess === '10'"
              :cpy-code="form.busMainData.otherSysOwnerCpy"
              :ori-org-code="form.busMainData.otherSysOrgCode"
              cpy-owner-type="custom"
              @cpySelectCallback="cpySelectCallback"
              @orgSetSelectCallback="orgSetSelectCallback" />
          </el-form-item>
          <el-form-item label="仓库" prop="busMainData.warehouseId">
            <WarehouseTreeSelect v-model="form.busMainData.warehouseId" :editable="form.busPageInfo.editable" style="width: 300px;" placeholder="选择仓库" @warehouseSelect="warehouseSelect" @setDefaultWarehouseId="setDefaultWarehouseId"/>
          </el-form-item>
          <el-form-item label="收货公司" prop="busMainData.receiveCpy">
            <el-input
              v-model="form.busMainData.receiveCpy"
              size="mini"
              class="filter-item" />
          </el-form-item>
          <el-form-item label="收货地址" prop="busMainData.receiveAddress">
            <el-input
              v-model="form.busMainData.receiveAddress"
              size="mini"
              class="filter-item" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="收货人" prop="busMainData.receivor">
            <el-input
              v-model="form.busMainData.receivor"
              size="mini"
              class="filter-item" />
          </el-form-item>
          <el-form-item label="收货电话" prop="busMainData.receivePhone">
            <el-input
              v-model="form.busMainData.receivePhone"
              size="mini"
              class="filter-item" />
          </el-form-item>
          <el-form-item label="备注" prop="busMainData.busRmk">
            <el-input
              v-model="form.busMainData.busRmk"
              size="mini"
              class="filter-item" />
          </el-form-item>
          <el-form-item label="附档" prop="attachmentList">
            <FileUpload
              ref="fileUpload"
              :editable="form.busPageInfo.editable"
              :sup_this="sup_this"
              :file-limit="5"
              :is-direct-upload="true"
              list-type="list"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-collapse v-model="activeNames">
      <el-collapse-item title="退换货产品" name="1">
        <stockout-detail ref="stockoutDetail" :sup-form="thisForm" :stockout-details="form.stockoutDetails" :warehouse-id="form.busMainData.warehouseId" :editable="form.busPageInfo.editable" @setDetails="setDetails" />
      </el-collapse-item>
      <el-collapse-item title="付款方式" name="2">
        <stock-payment-detail ref="stockPaymentDetails" :payment-details="form.stockPaymentDetails" :editable="form.busPageInfo.editable" :total-money="totalMoney" @setDetails="setPaymentDetails"/>
      </el-collapse-item>
    </el-collapse>
    <div v-if="form.busPageInfo.editable" slot="footer" class="modal-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','STOCKOUT_ORDER_REJECT_ALL','STOCKOUT_ORDER_REJECT_CHANGE','STOCKOUT_ORDER_REJECT_CREATE']" :loading="loading" type="primary" @click="doSave">暂存</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','STOCKOUT_ORDER_REJECT_ALL','STOCKOUT_ORDER_REJECT_SUBMIT']" :loading="loading" type="primary" @click="doSubmit">提交</el-button>
    </div>
  </div>
</template>

<script>
import stockoutOrder from '@/api/prod/StockoutOrder'
import StockoutDetail from './stockoutOrderDetail'
import CpyOrgSetSelectDialog from '@/views/components/CpyOrgSetSelectDialog'
import OrgsetTreeSelect from '@/views/components/OrgsetTreeSelect'
import WarehouseTreeSelect from '@/views/base/warehouse/module/WarehouseTreeSelect'

export default {
  components: { StockoutDetail, CpyOrgSetSelectDialog, OrgsetTreeSelect, WarehouseTreeSelect },
  dictTypes: ['logistics_cpy', 'logistics_status', 'sale_stock_billtype'],
  props: {
    sup_this: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      isAdd: false,
      loading: false,
      thisForm: this,
      style: 'width: 184px',
      activeNames: ['1'],
      dialogInfo: {
        name: 'stockoutOrderRejectApproveMngFormDialog',
        title: '销售退换单审核',
        fullScreen: true,
        width: 500,
        height: 90
      },
      form: {
        stockoutDetails: [],
        stockPaymentDetails: [],
        busMainData: {
          id: '',
          enableFlag: 'Y'
        },
        busPageInfo: {
          editable: false
        }
      },
      rules: {
        'busMainData.busDate': [
          { required: true, message: '请选择业务时间', trigger: 'blur' }
        ],
        'busMainData.sysOrgCode': [
          { required: true, message: '请选择机构', trigger: 'blur' }
        ],
        'busMainData.otherSysOwnerCpy': [
          { required: true, message: '请选择客户', trigger: 'blur' }
        ],
        'busMainData.warehouseId': [
          { required: true, message: '请选择仓库', trigger: 'blur' }
        ],
        'busMainData.billType': [
          { required: true, message: '请选择类型', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    totalMoney: function() {
      return this.getDetailTotalMoney()
    }
  },
  mounted() {
    this.$emit('afterMounted')
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore() {
    },
    viewApproveData(busId) {
      const data = { paramBean: { busId: busId }}
      stockoutOrder.toViewApprove(data).then(res => {
        this.form = res.data
        this.getOtherDetailData(this.form.busMainData.id, false, false)
      })
    },
    getOtherDetailData(mainId, replaceFlag, replaceDetailLotFlag) {
      if (mainId) {
        stockoutOrder.getDetailData(mainId, replaceFlag, replaceDetailLotFlag).then(res => {
          this.form.stockoutDetails = res.data
        })
        stockoutOrder.getPaymentDetailData(mainId, replaceFlag, replaceDetailLotFlag).then(res => {
          this.form.stockPaymentDetails = res.data
        })
      }
      setTimeout(() => {
        this.$refs.fileUpload.setFileList(this.form.attachmentList)
      }, 500)
    },
    cpySelectCallback(selectedRow, selectObj) {
      this.form.busMainData.otherSysOwnerCpy = ''
      this.form.busMainData.busOtherCpyName = ''
      if (selectedRow) {
        this.form.busMainData.otherSysOwnerCpy = selectedRow.cpyCode
        this.form.busMainData.busOtherCpyName = selectedRow.cpyLongName
      }
      // 更改供应商，清空明细
      this.form.stockoutDetails = []
      selectObj.cancel()
    },
    orgSetSelectCallback(orgCode) {
      this.form.busMainData.otherSysOrgCode = orgCode
    },
    setDetails(details) {
      this.form.stockoutDetails = details
      this.refreshPaymetDetail()
    },
    getDetailTotalMoney() {
      let totalMoney = 0
      this.form.stockoutDetails.forEach(tmprow => {
        totalMoney = totalMoney + (tmprow.money || 0)
      })
      return totalMoney
    },
    refreshPaymetDetail() {
      const totalMoney = this.getDetailTotalMoney()
      const refPaymentDetail = this.$refs.stockPaymentDetail
      this.form.stockPaymentDetails.forEach(tmprow => {
        refPaymentDetail.calPaymetDetail(totalMoney, tmprow)
      })
    },
    setPaymentDetails(details) {
      this.form.paymentDetails = details
    },
    warehouseSelect(warehouseId) {
      this.form.stockoutDetails.forEach(tmpdetail => {
        tmpdetail.storelocationId = null
      })
    },
    setDefaultWarehouseId(warehouseId) {
      this.form.busMainData.warehouseId = warehouseId
    }
  }
}
</script>
